jQuery.fn.configuraFormConsulta = function(){
    this.each(function(){
        
        $(":text", this).each(function(){
            var input1= $(this);
            var field_name = input1.attr("name");

            if(input1.attr("field_type") == "string"){
                // Cria o select
                var select = $("<select name=\""+field_name+"[criterio]"+"\"></select>");
                var options = "<option value=\"contem\">Cont&eacute;m</option>"+
                    "<option value=\"inicia\">Inicia em</option>"+
                    "<option value=\"termina\">Termina em</option>"+
                    "<option value=\"igual\">Igual</option>";
                select.append(options);
                
                // Armazena os componentes em relacao ao input1
                input1.after(select);
                input1.after("&nbsp;");
                
                // Configura o input1
                input1.attr("size", "46");
                input1.attr("name", field_name+"[campo1]");
            }else if(input1.attr("field_type") == "number"){
                //criacao do select
                var select = $("<select name=\""+field_name+"[criterio]"+"\"></select>");
                var options = "<option value=\"igual\">Igual</option>"+
                    "<option value=\"maior\">Maior que</option>"+
                    "<option value=\"menor\">Menor que</option>"+
                    "<option value=\"entre\">Entre</option>";
                select.append(options);
                select.change(selectEvent);
                
                // Insere o select antes do input1

                
                // Cria o input2
                var input2 = $("<input type=\"text\" disabled=\"disabled\"name=\""+field_name+"[campo2]"+"\"/>");
                input2.mask("?9999999999", {placeholder:""});
                select.data("input2", input2);
                
                // Armazena os componentes em relacao ao input1
                input1.after(select);
                input1.after("&nbsp;");
                input1.after(input2);
                input1.after("&nbsp;a&nbsp;");
                
                // Configura o input 1
                input1.mask("?9999999999", {placeholder:""});
                input1.attr("name", field_name+"[campo1]");
            }else if(input1.attr("field_type") == "date"){
                // Cria o select
                var select = $("<select name=\""+field_name+"[criterio]"+"\"></select>");
                var options = "<option value=\"igual\">Igual</option>"+
                    "<option value=\"maior\">Maior que</option>"+
                    "<option value=\"menor\">Menor que</option>"+
                    "<option value=\"entre\">Entre</option>";
                select.append(options);
                select.change(selectEvent);
                
                
                // Cria o input2
                var input2 = $("<input type=\"text\" maxLength=\"10\" disabled=\"disabled\"name=\""+field_name+"[campo2]"+"\"/>");
                input2.validaData();
                input2.mask("99/99/9999");
                select.data("input2", input2);
                
                // Armazena os componentes em relacao ao input1
                input1.after(select);
                input1.after("&nbsp;");
                input1.after(input2);
                input1.after("&nbsp;a&nbsp;");
                
                // Configura o input1
                input1.validaData();
                input1.mask("99/99/9999");
                input1.attr("name", field_name+"[campo1]");
            }else if(input1.attr("field_type") == "money"){
                // Cria o input
                var select = $("<select name=\""+field_name+"[criterio]"+"\"></select>");
                var options = "<option value=\"igual\">Igual</option>"+
                    "<option value=\"maior\">Maior que</option>"+
                    "<option value=\"menor\">Menor que</option>"+
                    "<option value=\"entre\">Entre</option>";
                select.append(options);
                select.change(selectEvent);
                
                // Cria o input 2
                var input2 = $("<input type=\"text\" maxLength=\"10\" disabled=\"disabled\"name=\""+field_name+"[campo2]"+"\"/>");
                input2.maskMoney({symbol:"R$ ", decimal:",",thousands:"."});
                select.data("input2", input2);
                
                // Armazena os componentes em relacao ao input1
                input1.after(select);
                input1.after("&nbsp;");
                input1.after(input2);
                input1.after("&nbsp;a&nbsp;");
                
                //Configura o input1
                input1.maskMoney({symbol:"R$ ", decimal:",",thousands:"."});
                input1.attr("name", field_name+"[campo1]");
            }
        });
        $("#input_avancado", this).click(habilitaTbodyAvancado);
        return jQuery;
    })
    
    function selectEvent(){
        var source = $(this);
        var input2 = $(source.data("input2"));
        if (source.val() == "entre"){
            input2.prop("disabled", false);
        }else{
            if (!input2.prop("disabled")){
                input2.prop("disabled", true);
            }
        }
    }
    
    function habilitaTbodyAvancado(){
        
        var tbody = $("#tbody_avancado");
        if (tbody.css("display") == "none"){
            tbody.css("display", "block");
        }else{
            tbody.css("display", "none");
            $(":text", tbody).val("");
            $("select", tbody).each(function(){
                $("option:selected",this).removeAttr("selected");
                $('option:eq(0)',this).attr("selected","selected");
                var input2 = $(this).data("input2");
                if(input2 != "undefined"){
                    $(input2).prop("disabled", true);
                }
            });
        }
    }

}